﻿using System;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Collections;
using Dcms.BLL;
using Dcms.Model;
using Dcms.Common;

public partial class Admin_AdminLogin : System.Web.UI.Page
{
    readonly string strBackKey = ConfigurationManager.AppSettings["BackKey"];
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            this.btnLogin.Attributes.Add("onclick", "return validateFrm();");
            CompetenceLib CL = new CompetenceLib();
            String Temp = CL.OutLogin();
            if (Temp != "")
            {
                this.UpdateProgress1.Style.Add("display", "");
                this.labMessage.Text = Temp;
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Admin_AdminLogin.Page_Load", ex);
            throw ex;
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            //string IsUCenter = ConfigurationManager.AppSettings["IsUCenter"].ToUpper();
            //if (IsUCenter == "TRUE")
            //{
            //    string strMsg = "";
            //    if (UCenterOperate.LoginUser(txtUserName.Text.Trim(), txtPassword.Text.Trim(), strBackKey, out strMsg) == true)
            //    {
            //        //设置UCCookie
            //        UserService.SetUserAuthIn(txtUserName.Text.Trim(), "MyUc");

            //        //设置系统本身的存贮方式，及判断用户是否已更新到应用表中
            //        Dcms_AdminBLL AdminBll = new Dcms_AdminBLL();
            //        Dcms_AdminModel admin = AdminBll.GetModelByName(txtUserName.Text.Trim());
            //        if (admin == null)
            //        {
            //            //将用户存入当前用户中
            //            admin = new Dcms_AdminModel();
            //            admin.Admin_AddTime = DateTime.Now;
            //            admin.Admin_Email = "";
            //            admin.Admin_LastIp = DcmsRequest.GetIP();
            //            admin.Admin_LastTime = DateTime.Now;
            //            admin.Admin_LoginTimes = 0;
            //            admin.Admin_Msn = "";
            //            admin.Admin_Name = txtUserName.Text.Trim();
            //            admin.Admin_Pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPassword.Text.Trim(), "md5").ToLower();
            //            admin.Admin_QQ = "";
            //            admin.Admin_RoleId = 0;
            //            AdminBll.AddModel(admin);
            //        }
            //        ArrayList CoderAl = AdminBll.GetCoders();
            //        CompetenceLib CL = new CompetenceLib();
            //        CL.setSessionModle(true, CoderAl.Contains(admin.Admin_Name), admin.Admin_Id, admin.Admin_Name, admin.Admin_RoleId);
            //        Response.Redirect("MainFrom.aspx", false);
            //        return;
            //    }
            //    this.UpdateProgress1.Style.Add("display", "");
            //    this.labMessage.Text = strMsg;
            //    return;
            //}
            //else
            //{
                Dcms_AdminBLL AdminBll = new Dcms_AdminBLL();
                Dcms_AdminModel admin = new Dcms_AdminModel();
                admin = AdminBll.CheckAdminLogin
                    (this.txtUserName.Text,
                     FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPassword.Text, "md5").ToLower());

                if (admin != null)
                {
                    ArrayList CoderAl = AdminBll.GetCoders();
                    CompetenceLib CL = new CompetenceLib();
                    CL.setSessionModle(true, CoderAl.Contains(admin.Admin_Name), admin.Admin_Id, admin.Admin_Name, admin.Admin_RoleId);
                    //if (this.txtPassword.Text == "123456")
                    //{
                    //    Session.Add("NewUser", true);
                    //}
                    //if (admin.Admin_Name == String.Empty)
                    //{
                    //    Session.Add("NewUser", true);
                    //}
                    //中止线程的错误解决方法Response.Redirect("MainFrom.aspx",false)
                    Response.Redirect("MainFrom.aspx", false);
                }
                else
                {
                    this.UpdateProgress1.Style.Add("display", "");
                    this.labMessage.Text = "用户名或密码错误！";
                }
            //}
            
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Admin_AdminLogin.btnLogin_Click", ex);
            throw ex;
        }
    }
}